<?php

namespace Welcomescreen\Objects;

class User {
	const PASSWORD_SALT = '2q3984sadkf';
	
	/**
	 * @var string
	 */
	protected $name;
	
	/**
	 * @var string
	 */
	protected $password;
	
	/**
	 * @param string $name
	 * @param string $password
	 */
	public function __construct($name, $password, $passwordIsHashed = true) {
		$this->setName($name);
		$this->setPassword($password, $passwordIsHashed);
	}
	
	/**
	 * @return string
	 */
	public function getName() {
		return $this->name;
	}
	
	/**
	 * @param string $name
	 */
	public function setName($name) {
		$this->name = $name;
	}
	
	/**
	 * @return string
	 */
	public function getPassword() {
		return $this->password;
	}
	
	/**
	 * @param string $password
	 * @param bool $isHashed
	 */
	public function setPassword($password, $isHashed = false) {
		if($isHashed) {
			$this->password = $password;
		} else {
			$this->password = self::hashPassword($password);
		}
	}
	
	/**
	 * Checks whether the given unencrypted password is equal to the password of this user.
	 *
	 * @param string $password 
	 * @return bool
	 */
	public function checkPassword($password) {
		return $this->password == self::hashPassword($password);
	}
	
	/**
	 * @param string $password
	 * @return string
	 */
	public static function hashPassword($password) {
		return sha1(self::PASSWORD_SALT . $password);
	}
}

?>